package com.cac.demo.service.msAoMatchInfo;

import com.cac.demo.dao.msAoMatchInfo.MsAoMatchInfoDao;
import com.cac.demo.entity.msAoMatchInfo.MsAoMatchInfoDTO;
import com.cac.demo.util.DateUtil;
import com.cac.demo.util.adapter.ComUtil;
import com.cac.demo.util.adapter.DaoException;
import com.cac.demo.util.adapter.QueryReqBean;
import com.cac.demo.util.adapter.QueryRespBean;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.text.ParseException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class MsAoMatchInfoService {

    @Autowired
    private MsAoMatchInfoDao msAoMatchInfoDao;

    //分拣配套页面数据查询
    public QueryRespBean<HashMap> searchByPage(QueryReqBean<HashMap> queryReqBean){
        QueryRespBean<HashMap> queryRespBean = new QueryRespBean<>();
        try{
            PageHelper.startPage(queryReqBean.getPageParameter());
            HashMap searchParams = queryReqBean.getSearchParams();
            Page<HashMap> dataList = msAoMatchInfoDao.searchMsAoMatchInfoByPage(searchParams);
            queryRespBean.setResult(dataList);
            return queryRespBean;
        }catch (Exception e){
            throw new DaoException(e.getMessage());
        }
    }

    //分拣配套数据新增
    @Transactional
    public int insertMsAoMatchInfoBasicList(List<Map> map) throws ParseException {
        for (Map maps : map) {
            String msMatchInfoId= ComUtil.getId();
            Date creationDate= DateUtil.nowTime();
            msAoMatchInfoDao.insertInfoParams(maps,msMatchInfoId);
        }
        try {
            return 1;
        } catch (Exception e) {
            throw new DaoException(e.getMessage(), e);
        }
    }

    //删除
    @Transactional
    public int deleteBasic(String msAoMatchInfoId) {
        if (StringUtils.isEmpty(msAoMatchInfoId)) {
            throw new DaoException("删除失败！传入的参数主键为null");
        }
        try {
            //记录日志
            MsAoMatchInfoDTO msAoMatchInfoDTO = findById(msAoMatchInfoId);
            if (msAoMatchInfoDTO == null) {
                throw new DaoException("删除失败！对象不存在");
            }
            //删除业务数据
            int count = msAoMatchInfoDao.deleteBasic(msAoMatchInfoId);
            return count;
        } catch (Exception e) {
            throw new DaoException(e.getMessage(), e);
        }
    }

    //批量删除
    @Transactional
    public int deletes(String[] ids) {
        int result = 0;
        for (String id : ids) {
            deleteBasic(id);
            result++;
        }
        return result;
    }

    private MsAoMatchInfoDTO findById(String msPartId){
        return msAoMatchInfoDao.searchMsAoMatchInfoId(msPartId);
    }


    public PageInfo selectMaterial(int startPage, int pageSize, HashMap queryParam){
        com.github.pagehelper.PageHelper.startPage(startPage,pageSize);
        //根据工作包ID查询其他查询条件 yebo 20230801修改，机型，批次，架次改为通过工作包ID查询
        HashMap m = msAoMatchInfoDao.queryProductByPackageId(queryParam);
        queryParam.putAll(m);
        List<HashMap> data = msAoMatchInfoDao.selectMaterial(queryParam);
        PageInfo pageInfo = new PageInfo(data);
        return pageInfo;
    }
}
